;V9990 ZOOM2
;
;This one for ZOOM >=1 (Zoom INTO the picture)
;
;HG 1994
;

wait	macro
	IN A,(#65)
	RRCA
	DB #38,#FB
	endm

setreg	macro @val
	if "@val">""
	ld a,@val
	endif
	out (#64),a
	endm

outreg	macro @val
	if "@val">""
	ld a,@val
	endif
	out (#63),a
	endm

ActPage equ #FAF6


BLOAD:	DB #FE
	DW BEGIN	
	DW END
	DW BEGIN
	ORG #D000

BEGIN:	JP ZoomIn
	JP ZoomIn

ZoomTab:DW #0100		;Init.Zoom.Factor
	DW #0004		;Zoom.change per frame
	DB 128			;Zoom X-Center
	DB 44			;Zoom Y-Center

ZoomIn: LD IX,ZoomTab
	LD DE,(#F7F8)
	LD (IX+2),E
	LD (IX+3),D
	LD E,(IX)
	LD D,(IX+1)
Loop3:	LD C,(IX+4)
	LD B,0
	push de
	CALL MBCDE
	LD A,(IX+4)
	SUB D
	LD H,A
	LD L,0			;HL = SX*256
	pop de
	LD B,0			;DY
Loop1:	ld a,(#fbec)
	and 4
	ret z
	CALL COPYX2
	ADD HL,DE		;Increase SX with invers Zoom factor
	INC B
	JR NZ,Loop1

	ld c,(IX+5)
	ld b,0
	push de
	call mbcde
	ld a,(IX+5)
	sub d
	ld h,a
	ld l,0
	pop de
	LD C,0			;DY
Loop2:	ld a,(#fbec)
	and 4
	ret z
	CALL CopyY2
	ADD HL,DE		;Increase SY with invers ZOOM factor
	INC C			;Increase DY
	LD A,C
	CP 212
	JR C,Loop2

	LD C,(IX+2)		;Change ZOOM factor
	LD B,(IX+3)
	EX DE,HL
	SBC HL,BC
	EX DE,HL
	JR NC,Loop3
	RET

CopyX2: IN A,(#65)
	RRCA
	JR C,CopyX2
	SETREG 32
	OUTREG H		;SX
	OUTREG 0
	OUTREG			;SY
	OUTREG
	OUTREG B		;DX
	OUTREG 0
	OUTREG			;DY
	OUTREG 1
	OUTREG			;NX
	OUTREG 0
	OUTREG 211		;NY
	OUTREG 0
	SETREG 52
	OUTREG #40
	RET

CopyY2: IN A,(#65)
	RRCA
	JR C,CopyY2
	SETREG 32
	OUTREG 0		;SX
	OUTREG
	OUTREG H		;SY
	OUTREG 1
	OUTREG 0		;DX
	OUTREG
	OUTREG C		;DY
	OUTREG (ActPage)
	OUTREG 0		;NX
	OUTREG 1
	OUTREG 1		;NY
	OUTREG 0
	SETREG 52
	OUTREG #40
	RET

MBCDE:	PUSH AF 	;OldFasion MULT. BCDE = BC*DE. (Unsigned)
	PUSH HL 	;It was the only one I could find in a hurry
	LD HL,0
	LD A,#10
MBDLP:	ADD HL,HL
	RL E
	RL D
	JR NC,MBDLPC
	ADD HL,BC
	JR NC,MBDLPC
	INC DE
MBDLPC: DEC A
	JR NZ,MBDLP
	EX DE,HL
	EX (SP),HL
	POP BC
	POP AF
	RET

END:	nop
